﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Yule8xCommon.Applib;
using Yule8xCommon.Db;

namespace Yule8xModel
{
    public class GiveCardModels
    {
        /// <summary>
        /// 钻石转入游戏
        /// <para>成功返回success, 失败返回失败代码</para>
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="game_id"></param>
        /// <param name="diamond_number"></param>
        /// <param name="game_mact_id"></param>
        /// <param name="payer_blance"></param>
        /// <returns></returns>
        public static bool ConvertDimondToGameCurrency(int uid, int game_id, int diamond_number, int game_currency_number, int game_mact_id, out string out_message)
        {
            out_message = "success";
            if (uid < 1 || game_id < 1 || diamond_number < 1 || game_mact_id < 1)
            {
                ResponseUtil.WriteEnd(string.Format("uid={0},gameid={1},number={2},mact_id={3}", uid, game_id, diamond_number, game_mact_id));
                out_message = "invalid_form"; // 表单不完整
                return false;
            }

            // 判断游戏ID在游戏服务器中是否存在
            string game_nickname = string.Empty;
            string unionid = string.Empty;

            if (string.IsNullOrEmpty(game_nickname))
            {
                switch (game_mact_id)
                {
                    case 1:
                        game_nickname = BozhongGameUtil.GetNickName(game_id);
                        break;
                    case 2:
                        game_nickname = WanghuGameUtil.GetNickName(game_id);
                        break;
                }
            }
            if (string.IsNullOrEmpty(game_nickname))
            {
                out_message = "invalid_gameid"; // GameID不存在
                return false;
            }

            // 判断钻石余额和转入数量
            string sqlstmt = string.Format("select top 1 diamond_number from t_user_finance_account where user_id={0}", uid);
            int diamond_blance = int.Parse(DbFactory.Init(AppInfo.conn_yule8x_agent).GetValue(sqlstmt, "0").ToString());
            if (diamond_number > diamond_blance)
            {
                out_message = "invalid_blance"; // 余额不足
                return false;
            }

            string game_field = string.Empty;
            switch (game_mact_id)
            {
                case 1:
                    // 如果moneyvalue数量不存在，则先往库里面写入一条记录
                    BozhongGameUtil.CheckMoneyValue(diamond_number, 0);
                    game_field = "game_id_bozhong";
                    break;
                case 2:
                    // 如果moneyvalue数量不存在，则先往库里面写入一条记录
                    WanghuGameUtil.CheckMoneyValue(game_currency_number);
                    game_field = "game_id_wanghu";
                    break;
            }
            // 只能赠送给普通用户，不能赠送给代理, 代理可以给自己赠送钻石，因为代理自己也可能玩游戏
            sqlstmt = string.Format("select top 1 user_id, is_agent, unionid from t_user where {1}={0}", game_id, game_field);
            System.Data.DataTable dtPayeeTemp = DbFactory.Init(AppInfo.conn_yule8x_agent).ExecuteDataTable(sqlstmt);
            if (dtPayeeTemp.Rows.Count > 0)
            {
                int uidtmp = DataTableUtil.GetInteger(dtPayeeTemp, "user_id");
                bool isAgent = DataTableUtil.GetInteger(dtPayeeTemp, "is_agent") > 0;
                unionid = DataTableUtil.GetString(dtPayeeTemp, "unionid");
                if (isAgent && uidtmp != uid)
                {
                    //out_message ="invalid_state"; // 只能赠送给普通用户，不能赠送给代理!
                    //return false;
                }
            }


            string userid = string.Empty;
            DbFactory.Init(AppInfo.conn_yule8x_agent, true);
            DbParams dbParams = DbFactory.CreateParams();
            dbParams.AddValue<int>("@pint_uid", uid);
            dbParams.AddValue<int>("@pint_dimond_number", diamond_number);
            dbParams.AddValue<int>("@pint_game_id", game_id);
            dbParams.AddValue<int>("@pint_game_mact_id", game_mact_id);
            dbParams.AddInputString("@pstr_game_nickname", game_nickname);
            dbParams.AddOutput<string>("@pstr_output");
            System.Data.Common.DbParameter[] outParams = DbFactory.Init(AppInfo.conn_yule8x_agent).ExecuteProcedureNonQuery("proc_diamond_transform", dbParams.ToArray());
            foreach (System.Data.Common.DbParameter p in outParams)
            {
                if (p.ParameterName == "@pstr_output")
                {
                    out_message = p.Value.ToString();
                    continue;
                }
            }
            bool result = (out_message.IndexOf("SUCCESS") > -1);
            if (result)
            {

                if (AppInfo.ForbidUpdateGameMode && AppInfo.IsDebugMode)
                {
                    // 必需是调试模式，禁止更新游戏数据库才有效，否则一不小心勾选了禁止更新游戏库，即使没有调试也会禁止更新，那就不行了
                    // 调试模式, (原则：游戏库只能读，不能修改) 直接跳过后续游戏库更新操作
                    out_message = string.Format("[调试模式:::钻石转入游戏] 游戏库只能读, 不能修改, 更新游戏库货币操作：gameid={0}, diamond_number={1},game_currency_number={2}, mact_id={3}", game_id, diamond_number, game_currency_number, game_mact_id);
                    LogUtil.Instance.WriteError(out_message, null);
                    game_mact_id = 0;
                }

                out_message = "success";
                if (game_mact_id == 1)
                {
                    // 增加到游戏服务器数据库中
                    bool result2 = BozhongGameUtil.UpdateRoomCard(game_id, game_currency_number);
                    if (!result2)
                    {
                        LogUtil.Instance.WriteInfo("[钻石转入游戏] 更新泊众房卡数据出错");
                    }
                }
                else if (game_mact_id == 2)
                {
                    GameUserInfoModel gameuser = WanghuGameUtil.GetGameUserInfo(game_id);
                    if (null == gameuser)
                    {
                        LogUtil.Instance.WriteInfo("[钻石转入游戏] 更新网狐游戏豆出错：用户不存在,game_id=" + game_id);
                    }
                    else
                    {
                        bool result2 = WanghuGameUtil.UpdateYouxidou(gameuser.UserID, gameuser.Accounts, game_currency_number, out out_message);
                        if (!result2)
                        {
                            LogUtil.Instance.WriteInfo("[钻石转入游戏] 更新网狐游戏豆出错: " + out_message);
                        }
                    }
                }
            }
            if (string.IsNullOrEmpty(out_message))
            {
                out_message = "success";
            }
            return result;
        }
    }
}
